{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# ipycanvas: John Conway's Game Of Life"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Some of the following code is adapted from https://jakevdp.github.io/blog/2013/08/07/conways-game-of-life/"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "source": [
        "%pip install -q ipycanvas"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "source": [
        "import asyncio\n",
        "\n",
        "import numpy as np\n",
        "\n",
        "from ipycanvas import RoughCanvas, hold_canvas"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "source": [
        "def life_step(x):\n",
        "    \"\"\"Game of life step\"\"\"\n",
        "    nbrs_count = sum(np.roll(np.roll(x, i, 0), j, 1)\n",
        "                     for i in (-1, 0, 1) for j in (-1, 0, 1)\n",
        "                     if (i != 0 or j != 0))\n",
        "    return (nbrs_count == 3) | (x & (nbrs_count == 2))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "source": [
        "def draw(x, canvas, color='black'):\n",
        "    with hold_canvas(canvas):\n",
        "        canvas.clear()\n",
        "        canvas.fill_style = '#FFF0C9'\n",
        "        canvas.rough_fill_style = 'solid'\n",
        "        canvas.fill_rect(-10, -10, canvas.width + 10, canvas.height + 10)\n",
        "        canvas.rough_fill_style = 'cross-hatch'\n",
        "\n",
        "        canvas.fill_style = color\n",
        "        canvas.stroke_style = color\n",
        "\n",
        "        living_cells = np.where(x)\n",
        "        \n",
        "        rects_x = living_cells[1] * n_pixels\n",
        "        rects_y = living_cells[0] * n_pixels\n",
        "\n",
        "        canvas.fill_rects(rects_x, rects_y, n_pixels)\n",
        "        canvas.stroke_rects(rects_x, rects_y, n_pixels)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "source": [
        "glider_gun =\\\n",
        "[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],\n",
        " [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],\n",
        " [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],\n",
        " [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],\n",
        " [1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n",
        " [1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],\n",
        " [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],\n",
        " [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\n",
        " [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]\n",
        "\n",
        "x = np.zeros((50, 70), dtype=bool)\n",
        "x[1:10,1:37] = glider_gun"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "source": [
        "n_pixels = 15\n",
        "\n",
        "canvas = RoughCanvas(width=x.shape[1]*n_pixels, height=x.shape[0]*n_pixels)\n",
        "canvas.fill_style = '#FFF0C9'\n",
        "canvas.rough_fill_style = 'solid'\n",
        "canvas.fill_rect(0, 0, canvas.width, canvas.height)\n",
        "\n",
        "canvas"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "source": [
        "draw(x, canvas, '#5770B3')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "trusted": true
      },
      "outputs": [],
      "source": [
        "for _ in range(300):\n",
        "    x = life_step(x)\n",
        "    draw(x, canvas, '#5770B3')\n",
        "\n",
        "    await asyncio.sleep(0.1)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python (Pyodide)",
      "language": "python",
      "name": "python"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "python",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8"
    },
    "orig_nbformat": 4,
    "toc-showcode": false
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
